//ws发送信息
export function wsSend(ws, data) {
  let msgBody = {
    type: data.type || "0x31",
    sTime: "",
    serial: "",
    content: data.content || ""
  };
  const params = JSON.stringify(msgBody);
    ws.readyState == ws.OPEN && ws.send(params)
}

//返回消息体
export function msgBody(data) {
  return {
    meetingId: data.meetingId,
    user: {
      memberId: data.memberId,
      memberType: "member"
    },
    kf: {
      waiterId: data.waiterId,
      waiterType: "waiter"
    },
    contentType: data.contentType,
    content: data.content || ""
  };
}

// 历史查询时间格式化 (yyyy-mm-ddTHH:mm:ss)
// 返回[当前时间，前一年的当前时间]
export function recoderTimeformat(date) {
  if (!date || !(date instanceof Date)) {
    date = new Date();
  }
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  const start = `${year - 1}-${month}-${day}T${hour}:${minute}:${second}`;
  const end = `${year}-${month}-${day}T${hour}:${minute}:${second}`;
  return [start, end];
}

//格式化查询时间
export function timeformat(date) {
  if (!date || !(date instanceof Date)) {
    date = new Date();
  }
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return `${year}-${month}-${day}T${hour}:${minute}:${second}`;
}


//格式化后端端时间戳
export function transTimestamp(timestamp) {
   let date = new Date()
  if (timestamp) {
    date = new Date(timestamp);
  }
  // const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  // const second = date.getSeconds();
  return `${addZero(month,2)}月${addZero(day,2)}日 ${addZero(hour,2)}:${addZero(minute,2)}`;
}


//会话内查询历史记录时间 传入时间戳 
// return [传出的时间，和前七天]
export function secondToDate(count=1,date) {
  const sevenDays = 1000*60*60*24*7*count
  let start,end = null 
  if (!date) {
    end = new Date();
    const endTime = end.getTime()
    start = new Date(endTime-sevenDays)
  }else{
    end = new Date(date);
    const endTime = end.getTime()
    start = new Date(endTime-sevenDays)
  }
  const startYear = start.getFullYear();
  const startMonth = start.getMonth() + 1;
  const startDay = start.getDate();
  const endYear = end.getFullYear();
  const endMonth = end.getMonth() + 1;
  const endDay = end.getDate();

  return[`${startYear}-${startMonth}-${startDay}T00:00:00`,`${endYear}-${endMonth}-${endDay}T23:59:59`] ;
}

// 转换消息体（将吸收到消息，转为页面渲染的消息）
export function transMessage(msg) {
  return {
    id: msg.serial,
    time: msg.sTime,
    content: msg.content.word
  };
}

// 转换消息体（将收到link类消息，转为页面渲染的消息
export function transLinkMessage (msg){
  const word  =  JSON.parse(msg.content.word)
  return {
    id: msg.serial,
    time: msg.sTime,
    content: word.toolName,
    toolUrl:word.toolUrl,
  };
}

// 转换消息体（将收到评价类消息，转为页面渲染的消息
export function transCommentMessage (msg){
  const word  =  JSON.parse(msg.content.word)

  return {
    id: msg.serial,
    time: msg.sTime,
    comment:`客户评价:【问题${ Number(word.isTrue) == 1 ? '已':'未'}解决】,【${Number(word.opinionRating)}颗星】${word.feedback ? `,【${word.feedback}】`:''}`
    // isTrue: word.isTrue, 
    // opinionRating: word.opinionRating,
    // feedback: word.feedback
  };
}
  //相邻会场客服和日期相同时进行合并
export  function merge(arr) {
  let index = undefined;
  for (let i = 0; i < arr.length - 1; i++) {
    const waiter0 = arr[i].find((item) => item.talkerType == "waiter");
    const waiter1 = arr[i + 1].find((item) => item.talkerType == "waiter");
    if (waiter0 && waiter1) { //比较相邻两个会场
      const time1 = waiter0.leavingTime.slice(0, 10);
      const time2 = waiter1.leavingTime.slice(0, 10);
      if (waiter0.talker === waiter1.talker && time1 === time2) {
        index = i;
        break;
      }
    }
  }
  if (index !== undefined) {
    const newArr = [];
    arr.forEach((item, j) => {
      if (j < index || j > index+1) {
        newArr.push(item);
      }
      if (j === index) {
        newArr.push(arr[j].concat(arr[j + 1]));
      }
    });
    return merge(newArr);
  } else {
    return  arr;
  }
}


// 高位补0 
export function addZero(num,length){
  return (Array(length).join('0')+num).slice(-length)
}